package com.meme.editor.property.value;

import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.meme.editor.property.Property;
import com.meme.editor.property.PropertyFactory;
import com.meme.editor.property.PropertyValue;

import com.meme.editor.EntityObject;
import com.meme.editor.EntityRefLink;

public class EntityLinkPropertyValue extends PropertyValue 
{
	static Logger logger = Logger.getLogger (EntityLinkPropertyValue.class);
	
	//EntityObject mCurrentEntity;
	EntityRefLink mEntityRefLink;
	Vector<EntityObject> mEntityList;	
	
	String mListNodePath;
	
	public EntityLinkPropertyValue (Property property)
	{
		super (property);
		
		mEntityRefLink = new EntityRefLink (getProperty().getOwner());
	}
	
	
	@Override
	public String getDefaultControlType ()
	{
		return PropertyFactory.ControlType_SelectList;
	}
	
	@Override
	public Object   getValue ()
	{
		//return mCurrentEntity;
		//return mEntityRefLink.getRefedEntity();
		
		//return mEntityRefLink;
		return mEntityRefLink.getRefedEntity();
	}
	
	@Override
	public String toString ()
	{
		//return mCurrentEntity.toString();
		return mEntityRefLink.isNull() ? "" : mEntityRefLink.getRefedEntity().toString();
	}
	
	@Override
	protected boolean modify (Object newValue)
	{
		if ( newValue == null )
		{
			mEntityRefLink.setRefedEntity ( null );
			return true;
		}
		else if ( newValue instanceof EntityObject )
		{
			mEntityRefLink.setRefedEntity ( (EntityObject) newValue );
			return true;
		}
		else if ( newValue instanceof String )
		{
			String strValue = (String) newValue;
			if (strValue.length() > 0)
			{
				createEntityList ();
				for (EntityObject entity : mEntityList)
				{
					if ( strValue.equals ( entity.getName() ) )
					{
						mEntityRefLink.setRefedEntity ( entity );
						return true;
					}
				}
			}
		}
		
		return false;
	}	
	
	@Override
	public String getValueAsString ()
	{
		return mEntityRefLink.isNull() ? "" : mEntityRefLink.getRefedEntity().getName();
	}

	@Override
	protected Object   makeLegalValueFromControl (Object[] params)
	{
		int selectIndex = ( (Integer) params[0] ).intValue();
		if ( selectIndex <= 0 )
			return null;
		else
		{
			return mEntityList.elementAt(selectIndex - 1);
		}
	}
	
	@Override
	protected Object[] makeDataForControl ()
	{
		Vector list = new Vector ();
		list.add ("");

		createEntityList ();
			
		if ( mEntityList != null )
		{
			list.addAll (mEntityList);
		}

		int selectIndex;
		if ( mEntityRefLink.isNull() )
			selectIndex = 0;
		else
		{
			logger.assertLog(mEntityList.contains(mEntityRefLink.getRefedEntity()), "Heng heng, somewhere error?!");
			logger.assertLog(mEntityRefLink.getRefedEntity().getParent().getChildren () == mEntityList, "Heng heng heng, somewhere error?!");
			
			
			selectIndex = 1 + mEntityRefLink.getRefedEntity().getIndex();
		}
		
		Object[] params = new Object[]
		      {
				selectIndex,
				list
		      };
		
		return params;
	}
	
	void createEntityList ()
	{
		if ( mEntityList == null )
		{
			try
			{
				EntityObject entity = (EntityObject) ( getProperty ().getOwner () );
				mEntityList = (Vector<EntityObject>) entity.parseNodePath(mListNodePath);
			}
			catch (Exception e)
			{
				logger.error ("Error when parsing mListNodePath.");
				logger.error (e);
				e.printStackTrace();
			}
		}
	}
	
	public final static String Param_NodePath = "entity_list";
	
	@Override
	protected boolean _setParameter (String name, Object value)
	{
		// defaultValue
		// canSelectNothing
		// canSelectMultiItems
		// listNodePath or listName
		
		if ( name.equals(Param_NodePath) )
		{
			mListNodePath = (String) value;
			
			return true;
		}
		
		return false;
	}

}
